home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 2.iso / dist / fw_glimpse.idb / usr / freeware / src / glimpse-3.0 / agrep / asearch.c.z / asearch.c
C/C++ Source or Header  |  1997-09-09  |  30KB  |  977 lines

  1. /* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal.  All Rights Reserved. */
  2. #include "agrep.h"
  3.  
  4. extern unsigned Init1, Init[], Mask[], endposition, D_endpos, AND, NO_ERR_MASK;
  5. extern int DELIMITER, FILENAMEONLY, INVERSE;
  6. extern CHAR CurrentFileName[];
  7. extern int I, num_of_matched, prev_num_of_matched, TRUNCATE;
  8.  
  9. extern int CurrentByteOffset;
  10. extern int errno;
  11. extern CHAR *agrep_inbuffer;
  12. extern int  agrep_inlen;
  13. extern int  agrep_initialfd;
  14. extern int  EXITONERROR;
  15. extern int  agrep_inpointer;
  16.  
  17. extern FILE *agrep_finalfp;
  18. extern CHAR *agrep_outbuffer;
  19. extern int agrep_outlen;
  20. extern int agrep_outpointer;
  21.  
  22. extern int NEW_FILE, POST_FILTER;
  23.  
  24. extern int LIMITOUTPUT, LIMITPERFILE;
  25.  
  26. int
  27. asearch(old_D_pat, text, D)
  28. CHAR old_D_pat[]; 
  29. int text; 
  30. register unsigned D;
  31. {
  32.     register unsigned i, c, r1, r2, CMask, r_NO_ERR, r_Init1; 
  33.     register unsigned A0, B0, A1, B1, endpos;
  34.     unsigned A2, B2, A3, B3, A4, B4;
  35.     unsigned A[MaxError+1], B[MaxError+1];
  36.     unsigned D_Mask;
  37.     int end;
  38.     int D_length, FIRSTROUND, ResidueSize, lasti, l, k, j=0;
  39.     int printout_end;
  40.     CHAR *buffer;
  41.     /* CHAR *tempbuf = NULL; */    /* used only when text == -1 */
  42.  
  43.     if (I == 0) Init1 = (unsigned)037777777777;
  44.     if(D > 4) {
  45.         return asearch0(old_D_pat, text, D); 
  46.     }
  47.  
  48.     D_length = strlen(old_D_pat);
  49.     D_Mask = D_endpos;
  50.     for ( i=1; i<D_length; i++) D_Mask = (D_Mask<<1) | D_Mask;
  51.     D_Mask = ~D_Mask;
  52.  
  53.     r_Init1 = Init1; /* put Init1 in register */
  54.     r_NO_ERR = NO_ERR_MASK; /* put NO_ERR_MASK in register */
  55.     endpos = D_endpos;    
  56.     FIRSTROUND = ON;
  57.     A0 = B0 = A1 = B1 = A2 = B2 = A3 = B3 = A4 = B4 = Init[0];
  58.     for(k=0; k<=D; k++) A[k] = B[k] = Init[0];
  59.  
  60. #if    AGREP_POINTER
  61.     if (text != -1) {
  62. #endif    /*AGREP_POINTER*/
  63.         lasti = Max_record;
  64.         alloc_buf(text, &buffer, Max_record+BlockSize+1);
  65.         buffer[Max_record-1] = '\n';
  66.  
  67.         while ((l = fill_buf(text, buffer + Max_record, BlockSize)) > 0)
  68.         {
  69.             i = Max_record;
  70.             end = Max_record + l ;
  71.             if (FIRSTROUND) { 
  72.                 i = Max_record - 1;
  73.                 if(DELIMITER) {
  74.                     for(k=0; k<D_length; k++) {
  75.                         if(old_D_pat[k] != buffer[Max_record+k]) break;
  76.                     }
  77.                     if(k>=D_length) j--;
  78.                 }
  79.                 FIRSTROUND = OFF; 
  80.             }
  81.             if (l < BlockSize) {    /* copy pattern and '\0' at end of buffer */
  82.                 strncpy(buffer+end, old_D_pat, D_length);
  83.                 buffer[end+D_length] = '\0';
  84.                 end = end + D_length; 
  85.             }
  86.  
  87.             /* ASEARCH_PROCESS: the while-loop below */
  88.             while (i < end )
  89.             {
  90.                 c = buffer[i];
  91.                 CMask = Mask[c];
  92.                 r1 = r_Init1 & B0;
  93.                 A0 = ((B0 >>1 ) & CMask) | r1;
  94.                 r1 = r_Init1 & B1;
  95.                 r2 =  B0 | (((A0 | B0) >> 1) & r_NO_ERR); 
  96.                 A1 = ((B1 >>1 ) & CMask) | r2 | r1 ;  
  97.                 if(D == 1) goto Nextcharfile;
  98.                 r1 = r_Init1 & B2;
  99.                 r2 =  B1 | (((A1 | B1) >> 1) & r_NO_ERR); 
  100.                 A2 = ((B2 >>1 ) & CMask) | r2 | r1 ;  
  101.                 if(D == 2) goto Nextcharfile;
  102.                 r1 = r_Init1 & B3;
  103.                 r2 =  B2 | (((A2 | B2) >> 1) & r_NO_ERR); 
  104.                 A3 = ((B3 >>1 ) & CMask) | r2 | r1 ;  
  105.                 if(D == 3) goto Nextcharfile;
  106.                 r1 = r_Init1 & B4;
  107.                 r2 =  B3 | (((A3 | B3) >> 1) & r_NO_ERR); 
  108.                 A4 = ((B4 >>1 ) & CMask) | r2 | r1 ;  
  109.                 if(D == 4) goto Nextcharfile;
  110. Nextcharfile:
  111.                 i=i+1;
  112.                 CurrentByteOffset ++;
  113.                 if(A0 & endpos) {
  114.                     j++;  
  115.                     if (DELIMITER) CurrentByteOffset -= D_length;
  116.                     else CurrentByteOffset -= 1;
  117.                     r1 = A0;
  118.                     if ( D == 1) r1 = A1;
  119.                     if ( D == 2) r1 = A2;
  120.                     if ( D == 3) r1 = A3;
  121.                     if ( D == 4) r1 = A4;
  122.                     if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
  123.                     {    
  124.                         if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
  125.                             num_of_matched++;
  126.                             if (agrep_finalfp != NULL) 
  127.                                 fprintf(agrep_finalfp, "%s\n", CurrentFileName);
  128.                             else {
  129.                                 int outindex;
  130.                                 for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) && 
  131.                                         (CurrentFileName[outindex] != '\0'); outindex++) {
  132.                                     agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
  133.                                 }
  134.                                 if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
  135.                                     OUTPUT_OVERFLOW;
  136.                                     /*
  137.                                     if (text == -1) {
  138.                                         memcpy(buffer+end-D_length, tempbuf, D_length+1);
  139.                                     }
  140.                                     */
  141.                                     free_buf(text, buffer);
  142.                                     return -1;
  143.                                 }
  144.                                 else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
  145.                                 agrep_outpointer += outindex;
  146.                             }
  147.                             /*
  148.                             if (text == -1) {
  149.                                 memcpy(buffer+end-D_length, tempbuf, D_length+1);
  150.                             }
  151.                             */
  152.                             free_buf(text, buffer);
  153.                             NEW_FILE = OFF;
  154.                             return 0;  
  155.                         }
  156.                         printout_end = i - D_length - 1 ; 
  157.                         if ((text != -1) && !(lasti >= Max_record + l - 1)) {
  158.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  159.                         }
  160.                         else if ((text == -1) && !(lasti >= l)) {
  161.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  162.                         }
  163.                         if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
  164.                             ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
  165.                             free_buf(text, buffer);
  166.                             return 0;    /* done */
  167.                         }
  168.                     }
  169.                     lasti = i - D_length; /* point to starting position of D_pat */
  170.                     TRUNCATE = OFF;
  171.                     for(k=0; k<= D; k++) {
  172.                         B[k] = Init[0];
  173.                     }
  174.                     r1 = B[0] & Init1;
  175.                     A[0] = (((B[0]>>1) & CMask) | r1) & D_Mask;
  176.                     for(k=1; k<= D; k++) {
  177.                         r1 = Init1 & B[k];
  178.                         r2 = B[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
  179.                         A[k] = (((B[k]>>1)&CMask) | r1 | r2) ;
  180.                     }
  181.                     A0 = A[0]; 
  182.                     B0 = B[0]; 
  183.                     A1 = A[1]; 
  184.                     B1 = B[1]; 
  185.                     A2 = A[2]; 
  186.                     B2 = B[2];
  187.                     A3 = A[3]; 
  188.                     B3 = B[3]; 
  189.                     A4 = A[4]; 
  190.                     B4 = B[4];
  191.                     if (DELIMITER) CurrentByteOffset += 1*D_length;
  192.                     else CurrentByteOffset += 1*1;
  193.                 }
  194.                 c = buffer[i];
  195.                 CMask = Mask[c];
  196.                 r1 = r_Init1 & A0;
  197.                 B0 = ((A0 >> 1 ) & CMask) | r1;
  198.                 /* printf("Mask = %o, B0 = %on", CMask, B0); */
  199.                 r1 = r_Init1 & A1;
  200.                 r2 =  A0 | (((A0 | B0) >> 1) & r_NO_ERR); 
  201.                 B1 = ((A1 >>1 ) & CMask) | r2 | r1 ;  
  202.                 if(D == 1) goto Nextchar1file;
  203.                 r1 = r_Init1 & A2;
  204.                 r2 =  A1 | (((A1 | B1) >> 1) & r_NO_ERR); 
  205.                 B2 = ((A2 >>1 ) & CMask) | r2 | r1 ;  
  206.                 if(D == 2) goto Nextchar1file;
  207.                 r1 = r_Init1 & A3;
  208.                 r2 =  A2 | (((A2 | B2) >> 1) & r_NO_ERR); 
  209.                 B3 = ((A3 >>1 ) & CMask) | r2 | r1 ;  
  210.                 if(D == 3) goto Nextchar1file;
  211.                 r1 = r_Init1 & A4;
  212.                 r2 =  A3 | (((A3 | B3) >> 1) & r_NO_ERR); 
  213.                 B4 = ((A4 >>1 ) & CMask) | r2 | r1 ;  
  214.                 if(D == 4) goto Nextchar1file;
  215. Nextchar1file:
  216.                 i=i+1;
  217.                 CurrentByteOffset ++;
  218.                 if(B0 & endpos) {
  219.                     j++;  
  220.                     if (DELIMITER) CurrentByteOffset -= D_length;
  221.                     else CurrentByteOffset -= 1;
  222.                     r1 = B0;
  223.                     if ( D == 1) r1 = B1;
  224.                     if ( D == 2) r1 = B2;
  225.                     if ( D == 3) r1 = B3;
  226.                     if ( D == 4) r1 = B4;
  227.                     if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
  228.                     { 
  229.                         if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
  230.                             num_of_matched++;
  231.                             free_buf(text, buffer);
  232.                             if (agrep_finalfp != NULL) 
  233.                                 fprintf(agrep_finalfp, "%s\n", CurrentFileName);
  234.                             else {
  235.                                 int outindex;
  236.                                 for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) && 
  237.                                         (CurrentFileName[outindex] != '\0'); outindex++) {
  238.                                     agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
  239.                                 }
  240.                                 if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
  241.                                     OUTPUT_OVERFLOW;
  242.                                     /*
  243.                                     if (text == -1) {
  244.                                         memcpy(buffer+end-D_length, tempbuf, D_length+1);
  245.                                     }
  246.                                     */
  247.                                     free_buf(text, buffer);
  248.                                     return -1;
  249.                                 }
  250.                                 else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
  251.                                 agrep_outpointer += outindex;
  252.                             }
  253.                             /*
  254.                             if (text == -1) {
  255.                                 memcpy(buffer+end-D_length, tempbuf, D_length+1);
  256.                             }
  257.                             */
  258.                             free_buf(text, buffer);
  259.                             NEW_FILE = OFF;
  260.                             return 0; 
  261.                         }
  262.                         printout_end = i - D_length - 1 ; 
  263.                         if((text != -1) && !(lasti >= Max_record + l - 1)) {
  264.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  265.                         }
  266.                         else if ((text == -1) && !(lasti >= l)) {
  267.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  268.                         }
  269.                         if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
  270.                             ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
  271.                             free_buf(text, buffer);
  272.                             return 0;    /* done */
  273.                         }
  274.                     }
  275.                     lasti = i - D_length ;
  276.                     TRUNCATE = OFF;
  277.                     for(k=0; k<= D; k++) {
  278.                         A[k] = Init[0];
  279.                     }
  280.                     r1 = A[0] & Init1; 
  281.                     B[0] = (((A[0]>>1)&CMask) | r1) & D_Mask;
  282.                     for(k=1; k<= D; k++) {
  283.                         r1 = Init1 & A[k];
  284.                         r2 = A[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
  285.                         B[k] = (((A[k]>>1)&CMask) | r1 | r2) ;
  286.                     }
  287.                     A0 = A[0]; 
  288.                     B0 = B[0]; 
  289.                     A1 = A[1]; 
  290.                     B1 = B[1]; 
  291.                     A2 = A[2]; 
  292.                     B2 = B[2];
  293.                     A3 = A[3]; 
  294.                     B3 = B[3]; 
  295.                     A4 = A[4]; 
  296.                     B4 = B[4];
  297.                     if (DELIMITER) CurrentByteOffset += 1*D_length;
  298.                     else CurrentByteOffset += 1*1;
  299.                 }
  300.             }
  301.  
  302.             if(l < BlockSize) {
  303.                 lasti = Max_record ;
  304.             }
  305.             else {
  306.                 ResidueSize = Max_record + l - lasti;
  307.                 if(ResidueSize > Max_record) {
  308.                     ResidueSize = Max_record;
  309.                     TRUNCATE = ON;         
  310.                 }
  311.                 strncpy(buffer+Max_record-ResidueSize, buffer+lasti, ResidueSize);
  312.                 lasti = Max_record - ResidueSize;
  313.                 if(lasti == 0)     lasti = 1; 
  314.             }
  315.         }
  316.         free_buf(text, buffer);
  317.         return 0;
  318. #if    AGREP_POINTER
  319.     }
  320.     else {
  321.         lasti = 1;
  322.         /* if (DELIMITER) tempbuf = (CHAR*)malloc(D_length + 1); */
  323.         buffer = (CHAR *)agrep_inbuffer;
  324.         l = agrep_inlen;
  325.         end = l;
  326.         /* buffer[end-1] = '\n'; */ /* at end of the text. */
  327.         /* buffer[0] = '\n'; */  /* in front of the  text. */
  328.         i = 0;
  329.         if(DELIMITER) {
  330.             for(k=0; k<D_length; k++) {
  331.                 if(old_D_pat[k] != buffer[k]) break;
  332.             }
  333.             if(k>=D_length) j--;
  334.             /*
  335.             memcpy(tempbuf, buffer+end, D_length+1);
  336.             strncpy(buffer+end, old_D_pat, D_length);
  337.             buffer[end+D_length] = '\0';
  338.             end = end + D_length;
  339.             */
  340.         }
  341.  
  342.             /* An exact copy of the above ASEARCH_PROCESS: the while-loop below */
  343.             while (i < end )
  344.             {
  345.                 c = buffer[i];
  346.                 CMask = Mask[c];
  347.                 r1 = r_Init1 & B0;
  348.                 A0 = ((B0 >>1 ) & CMask) | r1;
  349.                 r1 = r_Init1 & B1;
  350.                 r2 =  B0 | (((A0 | B0) >> 1) & r_NO_ERR); 
  351.                 A1 = ((B1 >>1 ) & CMask) | r2 | r1 ;  
  352.                 if(D == 1) goto Nextcharmem;
  353.                 r1 = r_Init1 & B2;
  354.                 r2 =  B1 | (((A1 | B1) >> 1) & r_NO_ERR); 
  355.                 A2 = ((B2 >>1 ) & CMask) | r2 | r1 ;  
  356.                 if(D == 2) goto Nextcharmem;
  357.                 r1 = r_Init1 & B3;
  358.                 r2 =  B2 | (((A2 | B2) >> 1) & r_NO_ERR); 
  359.                 A3 = ((B3 >>1 ) & CMask) | r2 | r1 ;  
  360.                 if(D == 3) goto Nextcharmem;
  361.                 r1 = r_Init1 & B4;
  362.                 r2 =  B3 | (((A3 | B3) >> 1) & r_NO_ERR); 
  363.                 A4 = ((B4 >>1 ) & CMask) | r2 | r1 ;  
  364.                 if(D == 4) goto Nextcharmem;
  365. Nextcharmem: 
  366.                 i=i+1;
  367.                 CurrentByteOffset ++;
  368.                 if(A0 & endpos) {
  369.                     j++;  
  370.                     if (DELIMITER) CurrentByteOffset -= D_length;
  371.                     else CurrentByteOffset -= 1;
  372.                     r1 = A0;
  373.                     if ( D == 1) r1 = A1;
  374.                     if ( D == 2) r1 = A2;
  375.                     if ( D == 3) r1 = A3;
  376.                     if ( D == 4) r1 = A4;
  377.                     if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
  378.                     {    
  379.                         if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
  380.                             num_of_matched++;
  381.                             if (agrep_finalfp != NULL) 
  382.                                 fprintf(agrep_finalfp, "%s\n", CurrentFileName);
  383.                             else {
  384.                                 int outindex;
  385.                                 for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) && 
  386.                                         (CurrentFileName[outindex] != '\0'); outindex++) {
  387.                                     agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
  388.                                 }
  389.                                 if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
  390.                                     OUTPUT_OVERFLOW;
  391.                                     /*
  392.                                     if (text == -1) {
  393.                                         memcpy(buffer+end-D_length, tempbuf, D_length+1);
  394.                                     }
  395.                                     */
  396.                                     free_buf(text, buffer);
  397.                                     return -1;
  398.                                 }
  399.                                 else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
  400.                                 agrep_outpointer += outindex;
  401.                             }
  402.                             /*
  403.                             if (text == -1) {
  404.                                 memcpy(buffer+end-D_length, tempbuf, D_length+1);
  405.                             }
  406.                             */
  407.                             free_buf(text, buffer);
  408.                             NEW_FILE = OFF;
  409.                             return 0;  
  410.                         }
  411.                         printout_end = i - D_length - 1 ; 
  412.                         if ((text != -1) && !(lasti >= Max_record + l - 1)) {
  413.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  414.                         }
  415.                         else if ((text == -1) && !(lasti >= l)) {
  416.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  417.                         }
  418.                         if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
  419.                             ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
  420.                             free_buf(text, buffer);
  421.                             return 0;    /* done */
  422.                         }
  423.                     }
  424.                     lasti = i - D_length; /* point to starting position of D_pat */
  425.                     TRUNCATE = OFF;
  426.                     for(k=0; k<= D; k++) {
  427.                         B[k] = Init[0];
  428.                     }
  429.                     r1 = B[0] & Init1;
  430.                     A[0] = (((B[0]>>1) & CMask) | r1) & D_Mask;
  431.                     for(k=1; k<= D; k++) {
  432.                         r1 = Init1 & B[k];
  433.                         r2 = B[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
  434.                         A[k] = (((B[k]>>1)&CMask) | r1 | r2) ;
  435.                     }
  436.                     A0 = A[0]; 
  437.                     B0 = B[0]; 
  438.                     A1 = A[1]; 
  439.                     B1 = B[1]; 
  440.                     A2 = A[2]; 
  441.                     B2 = B[2];
  442.                     A3 = A[3]; 
  443.                     B3 = B[3]; 
  444.                     A4 = A[4]; 
  445.                     B4 = B[4];
  446.                     if (DELIMITER) CurrentByteOffset += 1*D_length;
  447.                     else CurrentByteOffset += 1*1;
  448.                 }
  449.                 c = buffer[i];
  450.                 CMask = Mask[c];
  451.                 r1 = r_Init1 & A0;
  452.                 B0 = ((A0 >> 1 ) & CMask) | r1;
  453.                 /* printf("Mask = %o, B0 = %on", CMask, B0); */
  454.                 r1 = r_Init1 & A1;
  455.                 r2 =  A0 | (((A0 | B0) >> 1) & r_NO_ERR); 
  456.                 B1 = ((A1 >>1 ) & CMask) | r2 | r1 ;  
  457.                 if(D == 1) goto Nextchar1mem;
  458.                 r1 = r_Init1 & A2;
  459.                 r2 =  A1 | (((A1 | B1) >> 1) & r_NO_ERR); 
  460.                 B2 = ((A2 >>1 ) & CMask) | r2 | r1 ;  
  461.                 if(D == 2) goto Nextchar1mem;
  462.                 r1 = r_Init1 & A3;
  463.                 r2 =  A2 | (((A2 | B2) >> 1) & r_NO_ERR); 
  464.                 B3 = ((A3 >>1 ) & CMask) | r2 | r1 ;  
  465.                 if(D == 3) goto Nextchar1mem;
  466.                 r1 = r_Init1 & A4;
  467.                 r2 =  A3 | (((A3 | B3) >> 1) & r_NO_ERR); 
  468.                 B4 = ((A4 >>1 ) & CMask) | r2 | r1 ;  
  469.                 if(D == 4) goto Nextchar1mem;
  470. Nextchar1mem: 
  471.                 i=i+1;
  472.                 CurrentByteOffset ++;
  473.                 if(B0 & endpos) {
  474.                     j++;  
  475.                     if (DELIMITER) CurrentByteOffset -= D_length;
  476.                     else CurrentByteOffset -= 1;
  477.                     r1 = B0;
  478.                     if ( D == 1) r1 = B1;
  479.                     if ( D == 2) r1 = B2;
  480.                     if ( D == 3) r1 = B3;
  481.                     if ( D == 4) r1 = B4;
  482.                     if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
  483.                     { 
  484.                         if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
  485.                             num_of_matched++;
  486.                             free_buf(text, buffer);
  487.                             if (agrep_finalfp != NULL) 
  488.                                 fprintf(agrep_finalfp, "%s\n", CurrentFileName);
  489.                             else {
  490.                                 int outindex;
  491.                                 for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) && 
  492.                                         (CurrentFileName[outindex] != '\0'); outindex++) {
  493.                                     agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
  494.                                 }
  495.                                 if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
  496.                                     OUTPUT_OVERFLOW;
  497.                                     /*
  498.                                     if (text == -1) {
  499.                                         memcpy(buffer+end-D_length, tempbuf, D_length+1);
  500.                                     }
  501.                                     */
  502.                                     free_buf(text, buffer);
  503.                                     return -1;
  504.                                 }
  505.                                 else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
  506.                                 agrep_outpointer += outindex;
  507.                             }
  508.                             /*
  509.                             if (text == -1) {
  510.                                 memcpy(buffer+end-D_length, tempbuf, D_length+1);
  511.                             }
  512.                             */
  513.                             free_buf(text, buffer);
  514.                             NEW_FILE = OFF;
  515.                             return 0; 
  516.                         }
  517.                         printout_end = i - D_length - 1 ; 
  518.                         if((text != -1) && !(lasti >= Max_record + l - 1)) {
  519.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  520.                         }
  521.                         else if ((text == -1) && !(lasti >= l)) {
  522.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  523.                         }
  524.                         if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
  525.                             ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
  526.                             free_buf(text, buffer);
  527.                             return 0;    /* done */
  528.                         }
  529.                     }
  530.                     lasti = i - D_length ;
  531.                     TRUNCATE = OFF;
  532.                     for(k=0; k<= D; k++) {
  533.                         A[k] = Init[0];
  534.                     }
  535.                     r1 = A[0] & Init1; 
  536.                     B[0] = (((A[0]>>1)&CMask) | r1) & D_Mask;
  537.                     for(k=1; k<= D; k++) {
  538.                         r1 = Init1 & A[k];
  539.                         r2 = A[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
  540.                         B[k] = (((A[k]>>1)&CMask) | r1 | r2) ;
  541.                     }
  542.                     A0 = A[0]; 
  543.                     B0 = B[0]; 
  544.                     A1 = A[1]; 
  545.                     B1 = B[1]; 
  546.                     A2 = A[2]; 
  547.                     B2 = B[2];
  548.                     A3 = A[3]; 
  549.                     B3 = B[3]; 
  550.                     A4 = A[4]; 
  551.                     B4 = B[4];
  552.                     if (DELIMITER) CurrentByteOffset += 1*D_length;
  553.                     else CurrentByteOffset += 1*1;
  554.                 }
  555.             }
  556.  
  557.         /*
  558.         if (DELIMITER) {
  559.             memcpy(buffer+end, tempbuf, D_length+1);
  560.             free(tempbuf);
  561.         }
  562.         */
  563.         return 0;
  564.     }
  565. #endif    /*AGREP_POINTER*/
  566. }
  567.  
  568. int
  569. asearch0(old_D_pat, text, D)
  570. CHAR old_D_pat[]; 
  571. int text; 
  572. register unsigned D;
  573. {
  574.     register unsigned i, c, r1, r2, CMask, r_NO_ERR, r_Init1,  end, endpos; 
  575.     unsigned A[MaxError+2], B[MaxError+2];
  576.     unsigned D_Mask;
  577.     int D_length, FIRSTROUND, ResidueSize, lasti, l, k, j=0;
  578.     int printout_end;
  579.     CHAR *buffer;
  580.     /* CHAR *tempbuf = NULL;*/    /* used only when text == -1 */
  581.  
  582.     D_length = strlen(old_D_pat);
  583.     D_Mask = D_endpos;
  584.     for ( i=1; i<D_length; i++) D_Mask = (D_Mask<<1) | D_Mask;
  585.     D_Mask = ~D_Mask;
  586.  
  587.     r_Init1 = Init1; /* put Init1 in register */
  588.     r_NO_ERR = NO_ERR_MASK; /* put NO_ERR_MASK in register */
  589.     endpos = D_endpos;    
  590.     FIRSTROUND = ON;
  591.     for(k=0; k<=D; k++) A[k] = B[k] = Init[0];
  592.  
  593. #if    AGREP_POINTER
  594.     if (text != -1) {
  595. #endif    /*AGREP_POINTER*/
  596.         lasti = Max_record;
  597.         alloc_buf(text, &buffer, BlockSize+Max_record+1);
  598.         buffer[Max_record-1] = '\n';
  599.         while ((l = fill_buf(text, buffer + Max_record, BlockSize)) > 0)
  600.         {
  601.             i = Max_record; 
  602.             end = Max_record + l ;
  603.             if (FIRSTROUND) { 
  604.                 i = Max_record - 1;
  605.                 FIRSTROUND = OFF; 
  606.             }
  607.             if (l < BlockSize) {
  608.                 strncpy(buffer+end, old_D_pat, D_length);
  609.                 buffer[end+D_length] = '\0';
  610.                 end = end + D_length; 
  611.             }
  612.  
  613.             /* ASEARCH0_PROCESS: the while-loop below */
  614.             while (i < end )
  615.             {
  616.                 c = buffer[i++];
  617.                 CurrentByteOffset ++;
  618.                 CMask = Mask[c];
  619.                 r1 = B[0] & r_Init1;
  620.                 A[0] = (((B[0] >> 1)) & CMask | r1 ) ;
  621.                 for(k=1; k<=D; k++) {
  622.                     r1 = r_Init1 & B[k];
  623.                     r2 = B[k-1] | (((A[k-1]|B[k-1])>>1) & r_NO_ERR);
  624.                     A[k] = ((B[k] >> 1) & CMask) | r2 | r1;
  625.                 }
  626.                 if(A[0] & endpos) {
  627.                     j++;  
  628.                     if (DELIMITER) CurrentByteOffset -= D_length;
  629.                     else CurrentByteOffset -= 1;
  630.                     r1 = A[D];
  631.                     if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
  632.                     {    
  633.                         if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
  634.                             num_of_matched++;
  635.                             if (agrep_finalfp != NULL) 
  636.                                 fprintf(agrep_finalfp, "%s\n", CurrentFileName);
  637.                             else {
  638.                                 int outindex;
  639.                                 for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) && 
  640.                                         (CurrentFileName[outindex] != '\0'); outindex++) {
  641.                                     agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
  642.                                 }
  643.                                 if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
  644.                                     OUTPUT_OVERFLOW;
  645.                                     /*
  646.                                     if (text == -1) {
  647.                                         memcpy(buffer+end-D_length, tempbuf, D_length+1);
  648.                                     }
  649.                                     */
  650.                                     free_buf(text, buffer);
  651.                                     return -1;
  652.                                 }
  653.                                 else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
  654.                                 agrep_outpointer += outindex;
  655.                             }
  656.                             /*
  657.                             if (text == -1) {
  658.                                 memcpy(buffer+end-D_length, tempbuf, D_length+1);
  659.                             }
  660.                             */
  661.                             free_buf(text, buffer);
  662.                             NEW_FILE = OFF;
  663.                             return 0;  
  664.                         }
  665.                         printout_end = i - D_length - 1;           
  666.                         if((text != -1) && !(lasti >= Max_record + l - 1)) {
  667.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  668.                         }
  669.                         else if ((text == -1) && !(lasti >= l)) {
  670.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  671.                         }
  672.                         if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
  673.                             ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
  674.                             free_buf(text, buffer);
  675.                             return 0;    /* done */
  676.                         }
  677.                     }
  678.                     lasti = i - D_length; /* point to starting position of D_pat */
  679.                     for(k=0; k<= D; k++) {
  680.                         B[k] = Init[0];
  681.                     }
  682.                     r1 = B[0] & r_Init1;
  683.                     A[0] = (((B[0]>>1) & CMask) | r1) & D_Mask;
  684.                     for(k=1; k<= D; k++) {
  685.                         r1 = Init1 & B[k];
  686.                         r2 = B[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
  687.                         A[k] = (((B[k]>>1)&CMask) | r1 | r2) ;
  688.                     }
  689.                     if (DELIMITER) CurrentByteOffset += 1*D_length;
  690.                     else CurrentByteOffset += 1*1;
  691.                 }
  692.                 c = buffer[i++];
  693.                 CurrentByteOffset ++;
  694.                 CMask = Mask[c];
  695.                 r1   = r_Init1 & A[0];
  696.                 B[0] = ((A[0] >> 1 ) & CMask) | r1;
  697.                 for(k=1; k<=D; k++) {
  698.                     r1 = r_Init1 & A[k];
  699.                     r2 = A[k-1] | (((A[k-1]|B[k-1])>>1) & r_NO_ERR);
  700.                     B[k] = ((A[k] >> 1) & CMask) | r2 | r1;
  701.                 }
  702.                 if(B[0] & endpos) {
  703.                     j++;  
  704.                     if (DELIMITER) CurrentByteOffset -= D_length;
  705.                     else CurrentByteOffset -= 1;
  706.                     r1 = B[D];
  707.                     if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
  708.                     { 
  709.                         if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
  710.                             num_of_matched++;
  711.                             if (agrep_finalfp != NULL) 
  712.                                 fprintf(agrep_finalfp, "%s\n", CurrentFileName);
  713.                             else {
  714.                                 int outindex;
  715.                                 for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) && 
  716.                                         (CurrentFileName[outindex] != '\0'); outindex++) {
  717.                                     agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
  718.                                 }
  719.                                 if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
  720.                                     OUTPUT_OVERFLOW;
  721.                                     /*
  722.                                     if (text == -1) {
  723.                                         memcpy(buffer+end-D_length, tempbuf, D_length+1);
  724.                                     }
  725.                                     */
  726.                                     free_buf(text, buffer);
  727.                                     return -1;
  728.                                 }
  729.                                 else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
  730.                                 agrep_outpointer += outindex;
  731.                             }
  732.                             /*
  733.                             if (text == -1) {
  734.                                 memcpy(buffer+end-D_length, tempbuf, D_length+1);
  735.                             }
  736.                             */
  737.                             free_buf(text, buffer);
  738.                             NEW_FILE = OFF;
  739.                             return 0; 
  740.                         }
  741.                         printout_end = i - D_length -1 ; 
  742.                         if((text != -1) && !(lasti >= Max_record + l - 1)) {
  743.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  744.                         }
  745.                         else if ((text == -1) && !(lasti >= l)) {
  746.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  747.                         }
  748.                         if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
  749.                             ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
  750.                             free_buf(text, buffer);
  751.                             return 0;    /* done */
  752.                         }
  753.                     }
  754.                     lasti = i - D_length ;
  755.                     for(k=0; k<= D; k++) {
  756.                         A[k] = Init[0];
  757.                     }
  758.                     r1 = A[0] & r_Init1; 
  759.                     B[0] = (((A[0]>>1)&CMask) | r1) & D_Mask;
  760.                     for(k=1; k<= D; k++) {
  761.                         r1 = r_Init1 & A[k];
  762.                         r2 = A[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
  763.                         B[k] = (((A[k]>>1)&CMask) | r1 | r2) ;
  764.                     }
  765.                     if (DELIMITER) CurrentByteOffset += 1*D_length;
  766.                     else CurrentByteOffset += 1*1;
  767.                 }
  768.             }
  769.  
  770.             if(l < BlockSize) {
  771.                 lasti = Max_record;
  772.             }
  773.             else {
  774.                 ResidueSize = Max_record + l - lasti;
  775.                 if(ResidueSize > Max_record) {
  776.                     ResidueSize = Max_record;
  777.                     TRUNCATE = ON;         
  778.                 }
  779.                 strncpy(buffer+Max_record-ResidueSize, buffer+lasti, ResidueSize);
  780.                 lasti = Max_record - ResidueSize;
  781.                 if(lasti == 0)     lasti = 1; 
  782.             }
  783.         }
  784.         free_buf(text, buffer);
  785.         return 0;
  786. #if    AGREP_POINTER
  787.     }
  788.     else {
  789.         lasti = 1;
  790.         /* if (DELIMITER) tempbuf = (CHAR*)malloc(D_length + 1); */
  791.         buffer = (CHAR *)agrep_inbuffer;
  792.         l = agrep_inlen;
  793.         end = l;
  794.         /* buffer[end-1] = '\n';*/ /* at end of the text. */
  795.         /* buffer[0] = '\n';*/  /* in front of the  text. */
  796.         i = 0;
  797.         if(DELIMITER) {
  798.             for(k=0; k<D_length; k++) {
  799.                 if(old_D_pat[k] != buffer[k]) break;
  800.             }
  801.             if(k>=D_length) j--;
  802.             /*
  803.             memcpy(tempbuf, buffer+end, D_length+1);
  804.             strncpy(buffer+end, old_D_pat, D_length);
  805.             buffer[end+D_length] = '\0';
  806.             end = end + D_length;
  807.             */
  808.         }
  809.  
  810.             /* An exact copy of the above ASEARCH0_PROCESS: the while-loop below */
  811.             while (i < end )
  812.             {
  813.                 c = buffer[i++];
  814.                 CurrentByteOffset ++;
  815.                 CMask = Mask[c];
  816.                 r1 = B[0] & r_Init1;
  817.                 A[0] = (((B[0] >> 1)) & CMask | r1 ) ;
  818.                 for(k=1; k<=D; k++) {
  819.                     r1 = r_Init1 & B[k];
  820.                     r2 = B[k-1] | (((A[k-1]|B[k-1])>>1) & r_NO_ERR);
  821.                     A[k] = ((B[k] >> 1) & CMask) | r2 | r1;
  822.                 }
  823.                 if(A[0] & endpos) {
  824.                     if (DELIMITER) CurrentByteOffset -= D_length;
  825.                     else CurrentByteOffset -= 1;
  826.                     j++;  
  827.                     r1 = A[D];
  828.                     if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
  829.                     {    
  830.                         if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
  831.                             num_of_matched++;
  832.                             if (agrep_finalfp != NULL) 
  833.                                 fprintf(agrep_finalfp, "%s\n", CurrentFileName);
  834.                             else {
  835.                                 int outindex;
  836.                                 for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) && 
  837.                                         (CurrentFileName[outindex] != '\0'); outindex++) {
  838.                                     agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
  839.                                 }
  840.                                 if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
  841.                                     OUTPUT_OVERFLOW;
  842.                                     /*
  843.                                     if (text == -1) {
  844.                                         memcpy(buffer+end-D_length, tempbuf, D_length+1);
  845.                                     }
  846.                                     */
  847.                                     free_buf(text, buffer);
  848.                                     return -1;
  849.                                 }
  850.                                 else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
  851.                                 agrep_outpointer += outindex;
  852.                             }
  853.                             /*
  854.                             if (text == -1) {
  855.                                 memcpy(buffer+end-D_length, tempbuf, D_length+1);
  856.                             }
  857.                             */
  858.                             free_buf(text, buffer);
  859.                             NEW_FILE = OFF;
  860.                             return 0;  
  861.                         }
  862.                         printout_end = i - D_length - 1;           
  863.                         if((text != -1) && !(lasti >= Max_record + l - 1)) {
  864.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  865.                         }
  866.                         else if ((text == -1) && !(lasti >= l)) {
  867.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  868.                         }
  869.                         if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
  870.                             ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
  871.                             free_buf(text, buffer);
  872.                             return 0;    /* done */
  873.                         }
  874.                     }
  875.                     lasti = i - D_length; /* point to starting position of D_pat */
  876.                     for(k=0; k<= D; k++) {
  877.                         B[k] = Init[0];
  878.                     }
  879.                     r1 = B[0] & r_Init1;
  880.                     A[0] = (((B[0]>>1) & CMask) | r1) & D_Mask;
  881.                     for(k=1; k<= D; k++) {
  882.                         r1 = Init1 & B[k];
  883.                         r2 = B[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
  884.                         A[k] = (((B[k]>>1)&CMask) | r1 | r2) ;
  885.                     }
  886.                     if (DELIMITER) CurrentByteOffset += 1*D_length;
  887.                     else CurrentByteOffset += 1*1;
  888.                 }
  889.                 c = buffer[i++];
  890.                 CurrentByteOffset ++;
  891.                 CMask = Mask[c];
  892.                 r1   = r_Init1 & A[0];
  893.                 B[0] = ((A[0] >> 1 ) & CMask) | r1;
  894.                 for(k=1; k<=D; k++) {
  895.                     r1 = r_Init1 & A[k];
  896.                     r2 = A[k-1] | (((A[k-1]|B[k-1])>>1) & r_NO_ERR);
  897.                     B[k] = ((A[k] >> 1) & CMask) | r2 | r1;
  898.                 }
  899.                 if(B[0] & endpos) {
  900.                     j++;  
  901.                     if (DELIMITER) CurrentByteOffset -= D_length;
  902.                     else CurrentByteOffset -= 1;
  903.                     r1 = B[D];
  904.                     if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
  905.                     { 
  906.                         if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
  907.                             num_of_matched++;
  908.                             if (agrep_finalfp != NULL) 
  909.                                 fprintf(agrep_finalfp, "%s\n", CurrentFileName);
  910.                             else {
  911.                                 int outindex;
  912.                                 for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) && 
  913.                                         (CurrentFileName[outindex] != '\0'); outindex++) {
  914.                                     agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
  915.                                 }
  916.                                 if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
  917.                                     OUTPUT_OVERFLOW;
  918.                                     /*
  919.                                     if (text == -1) {
  920.                                         memcpy(buffer+end-D_length, tempbuf, D_length+1);
  921.                                     }
  922.                                     */
  923.                                     free_buf(text, buffer);
  924.                                     return -1;
  925.                                 }
  926.                                 else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
  927.                                 agrep_outpointer += outindex;
  928.                             }
  929.                             /*
  930.                             if (text == -1) {
  931.                                 memcpy(buffer+end-D_length, tempbuf, D_length+1);
  932.                             }
  933.                             */
  934.                             free_buf(text, buffer);
  935.                             NEW_FILE = OFF;
  936.                             return 0; 
  937.                         }
  938.                         printout_end = i - D_length -1 ; 
  939.                         if((text != -1) && !(lasti >= Max_record + l - 1)) {
  940.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  941.                         }
  942.                         else if ((text == -1) && !(lasti >= l)) {
  943.                             if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
  944.                         }
  945.                         if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
  946.                             ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
  947.                             free_buf(text, buffer);
  948.                             return 0;    /* done */
  949.                         }
  950.                     }
  951.                     lasti = i - D_length ;
  952.                     for(k=0; k<= D; k++) {
  953.                         A[k] = Init[0];
  954.                     }
  955.                     r1 = A[0] & r_Init1; 
  956.                     B[0] = (((A[0]>>1)&CMask) | r1) & D_Mask;
  957.                     for(k=1; k<= D; k++) {
  958.                         r1 = r_Init1 & A[k];
  959.                         r2 = A[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
  960.                         B[k] = (((A[k]>>1)&CMask) | r1 | r2) ;
  961.                     }
  962.                     if (DELIMITER) CurrentByteOffset += 1*D_length;
  963.                     else CurrentByteOffset += 1*1;
  964.                 }
  965.             }
  966.  
  967.         /*
  968.         if (DELIMITER) {
  969.             memcpy(buffer+end, tempbuf, D_length+1);
  970.             free(tempbuf);
  971.         }
  972.         */
  973.         return 0;
  974.     }
  975. #endif    /*AGREP_POINTER*/
  976. }
  977.